package com.videoaggre.zcl.ui.widget

import android.content.Context
import android.content.res.TypedArray
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.ViewCompat
import com.videoaggre.zcl.R
import com.videoaggre.zcl.common.dp
import com.videoaggre.zcl.common.toColor
import com.videoaggre.zcl.helper.OnValidationResultListener
import com.videoaggre.zcl.utils.Utils.notRepeatClick
import com.videoaggre.zcl.utils.VariableViewUtils

class CommonButton : AppCompatTextView, View.OnClickListener, OnValidationResultListener {
    companion object {
        const val CB_NORMAL = 1 // 直接使用主色调非空心
        const val CB_WHITE = 2 // 白色实心
        const val CB_STROKE_NORMAL = 3 // 主色调空心
        const val CB_BG = 4 // 背景调实心
        const val CB_MAIN_LIGHT = 5 // 主浅色调实心
        const val CB_STROKE_TEXT = 6 // 主字体色调空心
        const val CB_BLUE = 7 // 蓝色调实心
    }

    private var mOnClickListener: OnClickListener? = null
    private var defaultEnable = true// 是否活跃
    private var mVariableViewUtils: VariableViewUtils? = null
    private var normalDrawable: Drawable? = null
    private var enableFalseDrawable: Drawable? = null
    private var mCBStyle = CB_NORMAL
    private var radius = 30.dp.toFloat()
    private var stroke = 1.dp
    private var textSize = 16.dp

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        setOnClickListener(this)
        init(attrs)
    }

    fun init(attrs: AttributeSet?) {
        attrs?.let {
            val typedArray: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonButton)
            mCBStyle = typedArray.getInt(R.styleable.CommonButton_cb_style, CB_NORMAL)
            textSize = typedArray.getDimensionPixelSize(R.styleable.CommonButton_but_text_size, textSize)
            radius = typedArray.getDimension(R.styleable.CommonButton_but_radius, radius)
            typedArray.recycle()
        }
        changeStyle()
        enableFalseDrawable = GradientDrawable().apply {
            setColor(R.color.text_hint_color.toColor());cornerRadius = radius
        }
        setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize.toFloat())
        gravity = Gravity.CENTER
    }

    fun changeStyle(cbStyle: Int = mCBStyle) {
        when (cbStyle) {
            CB_NORMAL -> {
                normalDrawable = GradientDrawable().apply {
                    setColor(R.color.main_color.toColor());cornerRadius = radius
                }
                setTextColor(R.color.white.toColor())
            }
            CB_STROKE_NORMAL -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, R.color.main_color.toColor())
                }
                setTextColor(R.color.main_color.toColor())
            }
            CB_WHITE -> {
                normalDrawable = GradientDrawable().apply {
                    setColor(R.color.white.toColor());cornerRadius = radius
                }
                setTextColor(R.color.text_hint_color_2.toColor())
            }
            CB_BG -> {
                normalDrawable = GradientDrawable().apply {
                    setColor(R.color.bg.toColor());cornerRadius = radius
                }
                setTextColor(R.color.text_hint_color_2.toColor())
            }
            CB_STROKE_TEXT -> {
                normalDrawable = GradientDrawable().apply {
                    cornerRadius = radius
                    setStroke(stroke, R.color.text_color.toColor())
                }
                setTextColor(R.color.text_color.toColor())
            }
        }
        ViewCompat.setBackground(this, if (defaultEnable) normalDrawable else enableFalseDrawable)
    }

    override fun setOnClickListener(l: OnClickListener?) {
        if (this == l)
            super.setOnClickListener(this)
        else
            mOnClickListener = l
    }

    fun setVariableButtonUtils(variableViewUtils: VariableViewUtils) {
        mVariableViewUtils = variableViewUtils
    }

    override fun onClick(v: View?) {
        if (mOnClickListener != null && defaultEnable) {
            if (notRepeatClick()) {
                if (mVariableViewUtils == null) {
                    mOnClickListener?.onClick(v)
                } else if (mVariableViewUtils!!.clickValidator()) {
                    mOnClickListener?.onClick(v)
                }
            }
        }
    }

    override fun enable(enable: Boolean) {
        if (enable && !defaultEnable) ViewCompat.setBackground(this, normalDrawable)
        if (!enable && defaultEnable) ViewCompat.setBackground(this, enableFalseDrawable)
        defaultEnable = enable
    }
}